
//
//  DateTool.m
//  YiXiu
//
//  Created by yixiu on 16/7/29.
//  Copyright © 2016年 zzz. All rights reserved.
//

#import "Tools.h"

@implementation Tools




+ (NSString *)yyy_MM_ddWithDate:(NSDate *)date{
    NSDateFormatter * fm = [[NSDateFormatter alloc] init];
    fm.dateFormat = @"yyyy-MM-dd";
    return [fm stringFromDate:date];
}
+ (NSString *)MM_dd_HH_mm_ssWithDate:(NSDate *)date{
    NSDateFormatter * fm = [[NSDateFormatter alloc] init];
    fm.dateFormat = @"MM-dd HH:mm:ss";
    return [fm stringFromDate:date];
}



+ (NSDate *)yyy_MM_ddWithString:(NSString *)date{
    NSDateFormatter * fm = [[NSDateFormatter alloc] init];
    fm.dateFormat = @"yyyy-MM-dd";
    return [fm dateFromString:date];
}

+ (NSDate *)yyy_MM_dd_HH_mm_ssWithString:(NSString *)date{
    NSDateFormatter * fm = [[NSDateFormatter alloc] init];
    fm.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    return [fm dateFromString:date];
}

//Unicode转UTF-8
+ (NSString*) replaceUnicode:(NSString*)aUnicodeString

{
    if (!aUnicodeString) {
        return @"";
    }
    
    
    NSString *tempStr1 = [aUnicodeString stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
    
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
    
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString* returnStr = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NULL error:nil];
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}



//UTF-8 转码
+(NSString *) utf8ToUnicode:(NSString *)string{
    
    NSUInteger length = [string length];
    
    NSMutableString *s = [NSMutableString stringWithCapacity:0];
    
    for (int i = 0;i < length; i++){
        
        unichar _char = [string characterAtIndex:i];
        
        //判断是否为英文和数字
        if (_char <= '9' && _char >='0'){
            
            [s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
            
        }else if(_char >='a' && _char <= 'z'){
            
            [s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
            
        }else if(_char >='A' && _char <= 'Z'){
            
            [s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
            
        }else{
            
            [s appendFormat:@"\\u%x",[string characterAtIndex:i]];
        }
    }
    
    return s;
    
}


@end
